package com.hook3.pianke.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hook3.pianke.R;
import com.hook3.pianke.domain.TopicHotInfo;

import org.w3c.dom.Text;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/12.
 */
//创建一个listView和相应的item,创建一个类，可以完成数据的下载，装配到listView中
//然后在setAdapter进viewPager里
    //最新最热按钮点击只是设置viewpager伟当前页，数据是全部一次加入到viewPager中的，点击最新最热只是为了翻页！！
public class CommunityActivity {

    private Activity activity;
    private Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    private View mTitlecomm;
    private View mCommContent;
    public View view;
    public ViewPager vp_community_hotspot_news;
    private static final String[] titles = {"最新", "最热"};
    private List<TopicHotInfo> commInfos;
    private List<View> ll;
    public RelativeLayout rl_community_hotspotnews;
    public TextView tv_community_hot;
    public TextView tv_community_addTime;
    public ArrayList<TextView> textviewList;


    public ArrayList<CommunityBaseDataHuaTi> pagerList;
    public CommunityBaseDataHuaTi hotSpot;
    public CommunityBaseDataHuaTi addTime;

    public CommunityActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();

        //vp_community_hotspot_news.setAdapter(new CommunityAdapter());
//        ImageView imageView1 = new ImageView(activity);
//        ImageView imageView2 = new ImageView(activity);
//        imageView1.setBackgroundResource(R.mipmap.ic_back_black);
//        imageView2.setBackgroundResource(R.mipmap.ic_fragment_fm_msking);
//        list1.add(imageView1);
//        list1.add(imageView2);

        //填装到viewPager
        vp_community_hotspot_news.setAdapter(new MyHuatiAdapter());


        vp_community_hotspot_news.setOnPageChangeListener(new CommunityViewPagerListner());
        frameLayout.addView(rl_community_hotspotnews);

        textClick();

    }


    //设置viewPagerlistener
    class CommunityViewPagerListner implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            TextView textView = textviewList.get(position);

            textView.setBackgroundResource(R.drawable.selector_community_textview);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
    public ArrayList<CommunityBaseDataHuaTi> communitybaseData;
    private void textClick() {
        tv_community_hot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                vp_community_hotspot_news.setCurrentItem(0);
               //创建一个listView和相应的item,创建一个类，可以完成数据的下载，装配到listView中
                //然后在setAdapter进viewPager里

            }
        });



        tv_community_addTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                vp_community_hotspot_news.setCurrentItem(1);

            }
        });
    }

    /**
     * 实例化自己要实现的内容
     */


    public void init() {
        toolbar.setTitle("");

        //设置标题
        mTitlecomm = View.inflate(activity, R.layout.community_title, null);
        mTitlecomm.setOnClickListener(new MyOnClickListener());

        toolbar.addView(mTitlecomm);
        toolbar.setTag(mTitlecomm);
        mCommContent = View.inflate(activity, R.layout.community_context, null);
        ll = new ArrayList<>();
        //viewpager = (ViewPager) mCommContent.findViewById(R.id.viewpager_comm);

        for (int i = 0; i < titles.length; i++) {
            TextView textView = new TextView(activity);
            textView.setText(titles[i]);
            textView.setTextSize(20);
            ll.add(textView);


        }
        // viewpager.setAdapter(new MyCommPaerAdapter());


        // frameLayout.addView(mCommContent);
        //初始化视图
        view = View.inflate(activity, R.layout.community_hotspot_new, null);
        rl_community_hotspotnews = (RelativeLayout) view.findViewById(R.id.rl_community_hotspotnews);
        vp_community_hotspot_news = (ViewPager) view.findViewById(R.id.vp_community_hotspot_news);
        tv_community_hot = (TextView) view.findViewById(R.id.tv_community_hot);
        tv_community_addTime = (TextView) view.findViewById(R.id.tv_community_addTime);
        textviewList = new ArrayList<TextView>();
        textviewList.add(tv_community_hot);
        textviewList.add(tv_community_addTime);


        //得到数据集合装配到viewPager
        hotSpot = new CommunityBaseDataHuaTi(activity,"http://api2.pianke.me/group/posts_hotlist","hot");
        addTime = new CommunityBaseDataHuaTi(activity,"http://api2.pianke.me/group/posts_hotlist","addtime");
        pagerList = new ArrayList<CommunityBaseDataHuaTi>();
        pagerList.add(hotSpot);
        pagerList.add(addTime);
    }

    /**
     * 设置点击
     */
    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            showPopupWindow(v);

        }
    }

    //adapter设置
//   public class CommunityAdapter extends PagerAdapter {
//        @Override
//        public int getCount() {
//            return readDetalsCommonList.size();
//        }
//        @Override
//        public CharSequence getPageTitle(int position) {
//            // TODO Auto-generated method stub
//            return CONTENT[position] ;
//        }
//        @Override
//        public boolean isViewFromObject(View view, Object object) {
//            return view==object;
//        }
//
//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            ReadDetalsCommon readDetalsCommon =  readDetalsCommonList.get(position);
//            readDetalsCommon.initView();
//            readDetalsCommon.initData();
//            View view =  readDetalsCommon.view;
//
//            container.addView(view);
//
//            return view;
//        }
//
//        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView((View)object);
//        }
//    }
    /**
     * 设置pupwindow
     *
     * @param v
     */
    private PopupWindow popupWindow;
    public String Huatiurl;
    public String sort;
    public ArrayList<ImageView> list1 = new ArrayList<ImageView>();
    //练习数据


    private void showPopupWindow(View v) {
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.pop_window, null);
        // 设置按钮的点击事件
        Button button1 = (Button) contentView.findViewById(R.id.btn_pop1);
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "进入话题页面",
                        Toast.LENGTH_SHORT).show();
                //三个页面的网址不一样

                popupWindow.dismiss();
            }
        });


        Button button2 = (Button) contentView.findViewById(R.id.btn_pop2);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "进入小组页面",
                        Toast.LENGTH_SHORT).show();



                popupWindow.dismiss();
            }
        });
        Button button3 = (Button) contentView.findViewById(R.id.btn_pop3);
        button3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                Toast.makeText(activity, "进入我的页面",
//                        Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置好参数之后再show
        popupWindow.showAsDropDown(v, 0, toolbar.getHeight() - v.getHeight() - (toolbar.getHeight() / 2 - v.getHeight() / 2));
    }

    class MyHuatiAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return pagerList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            CommunityBaseDataHuaTi communityBaseDataHuaTi = pagerList.get(position);
            communityBaseDataHuaTi.initView();
            communityBaseDataHuaTi.initData();
            View view =communityBaseDataHuaTi.view;
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
            container.removeView((View)object);
        }
    }


}